<template>
  <div class="users">
    <el-row>
      <el-input
        style="width: 250px; margin-right: 20px"
        placeholder="请输入内容"
        v-model="userObj.query"
      >
        <el-button slot="append" icon="el-icon-search" @click="sreachUser"></el-button>
      </el-input>
      <el-button size="small" type="primary" @click="addUser">添加用户</el-button>
    </el-row>
    <el-row style="margin-top: 20px">
      <el-table :data="userList" border>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="username" label="姓名"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column prop="mobile" label="电话"></el-table-column>
        <el-table-column prop="role_name" label="角色"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <div>
              <el-switch
                v-model="scope.row.mg_state"
                active-color="#00f"
                inactive-color="#ccc"
                @change="changeState(scope.row)"
              >
              </el-switch>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div>
              <el-button
                @click="edit(scope.row)"
                size="mini"
                icon="el-icon-edit"
                type="primary"
                circle
              ></el-button>
                <el-popconfirm
                title="是否确认删除此数据"
                @confirm="delItem(scope.row)"
              >
                <el-button
                slot="reference"
                size="mini"
                icon="el-icon-delete"
                type="danger"
                circle
              ></el-button>
              </el-popconfirm>
              
              <el-button
                @click="set(scope.row)"
                size="mini"
                icon="el-icon-setting"
                type="warning"
                circle
              ></el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row style="margin-top: 20px" >
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[5, 10, 15]"
        :page-size="pageSize"
        layout=" sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="changeSize"
        @current-change="currChange"
      >
      </el-pagination>
    </el-row>
    <add-user :formData="formData" :formTitle="titleMsg" :dialogFormVisible="formFlag" @cancel="cancel" @confirm="confirm"></add-user>
    <RuleUser :ruleVis="ruleVis" :roleObj="roleObj" @cancel="roleCancel" @confirm="roleConfirm"></RuleUser>

  </div>
</template>

<script>
import { getUserList,changeUserState , delUser} from "../../api";
import AddUser from "./AddUsers.vue"
import RuleUser from "../../components/users/RuleUser.vue"
export default {
  name: "users",
  created() {
    this.getUserList(this.userObj);
  },
  components:{
    AddUser,
    RuleUser
  },
  data() {
    return {
      userList: [],
      userObj:{
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      pageSize:5,
      total:0,
      currentPage:1,
      titleMsg:"添加用户",
      formFlag:false,
      formData:{},
      roleObj:{},
      ruleVis:false
    };
  },
  methods: {
    // 获取表格数据
    getUserList(data) {
      getUserList(data).then((res) => {
        console.log(res);
        this.total = res.data.total
        this.userList = res.data.users;
      });
    },
    // 表格数据修改按钮
    edit(data) {
      console.log(data)
      this.titleMsg = "修改用户"
      this.formData = {
        username:data.username,
        email:data.email,
        mobile:data.mobile,
        id:data.id
      }
      this.formFlag = true
    },
    // 表格数据删除按钮
    delItem(data) {
      delUser(data.id).then(res=>{
        this.getUserList(this.userObj)
      })
    },
    // 表格数据设置按钮
    set(data) {
      console.log(data)
      this.roleObj = data
      this.ruleVis = true
    },
    // 查找用户
    sreachUser(){
      this.getUserList(this.userObj)
    },
    // 修改用户状态
    changeState(d){
      changeUserState({
        uid:d.id,
        type:d.mg_state
      }).then(() => {
        this.$message.success("设置成功")
      })
    },
    // 添加新用户
    addUser(){
      // this.$router.push("/layout/adduser")
      this.titleMsg = "添加用户"
      this.formData = {}
      this.formFlag = true
    },
    // 取消按钮
    cancel(){
      this.formFlag = false
    },
    // 确认按钮
    confirm(){
      this.formFlag = false
      this.getUserList(this.userObj)
    },
    // 改变每页条数
    changeSize(v){
      this.userObj.pagesize = v
      this.getUserList(this.userObj)
    },
    // 改变当前页码
    currChange(v){
      this.userObj.pagenum = v
      this.getUserList(this.userObj)
    },
    roleCancel(){
      this.ruleVis = false
    },
    roleConfirm(){
      this.ruleVis = false
      this.getUserList(this.userObj)
    }
  },
};
</script>

<style scoped>
.users {
  padding: 20px;
  margin-top: 20px;
  background: #fff;
}
</style>